﻿/* 变量 */
var activity = "activity/wechatAcitvity.html";
var activityDetail = "activity/wechatAcitvityDetail.html";
var generalActivityRegister = "activity/wechatAcitvityRegister.html";
var filedata = null;
var btnbottom=0;
var agent = "";
var contactsCallback_generalactivity = myApp.onPageInit('generalactivity', function (page) {

    //非微信浏览器下  不显示标题
    if (!ISS.utils.isWeiXin()) {
        mainView.hideNavbar();
        $$('.page-content').attr('style', 'margin-top:-44px')
    }
    //获取页面参数
    var userID = ISS.utils.getParameter("userID");
    if (userID && userID != undefined && userID != null && userID != "") {
        iss_localStorage.set("yuqing_appUserId", userID);
        mainView.hideNavbar();
        $$('.page-content').css('style', 'margin-top:-44px')
    }

    // 过滤
    $$(".navbar-inner .right img").hide();

    //删除滚动后重新添加滚动
    var flag_InfiniteScroll = true;
    //是否需要滚动加载
    var flag_needScroll = true;
    //滚动事件只执行一次
    var flag_execute = false;

    var index_tab1 = 1;
    var current_tabindex = 0;
    var size = 10;

    LoadActivityAll(index_tab1, size);
    //加载数据
    function LoadActivityAll(pageindex, pagesize) {
        var Account = ISS.utils.getParameter("Account");
        var _content = JSON.stringify(
                        {
                            AccountGuid: iss_localStorage.get("yuqing_appAccount")
                            //OpenId: iss_localStorage.get("yuqing_appOpenId")
                        });
        $$.ajax({
            url: ISS_Constants.Url.const_weixin_WechatAcitvity_get,
            data: { conditionParm: _content },
            crossDomain: true,
            dataType: "json",
            success: function (data, status) {
                if (data && data.datas && data.datas.length > 0) {
                    for (var i = 0; i < data.datas.length; i++) {
                        data.datas[i].ActivityBeginTime = ISS.utils.dateToStr("yyyy-MM-dd", ISS.utils.strToDate(data.datas[i].ActivityBeginTime));
                        data.datas[i].ActivityEndTime = ISS.utils.dateToStr("yyyy-MM-dd", ISS.utils.strToDate(data.datas[i].ActivityEndTime));
                    }
                    if (current_tabindex == 0) {
                        var searchTemplate = $$('#template-generalactivity-tab1').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#generalactivity_page #sub_stay_tab1 .content-block ul").append(html);
                            flag_InfiniteScroll = flag_needScroll = true;
                        }
                    }
                    //tab1综合,无限滚动只加载一次
                    if (!flag_execute) {
                        //只执行一次
                        flag_execute = true;
                        // 加载flag
                        var loading = false;
                        $$('.infinite-scroll').on('infinite', function () {
                            //综合 价格 距离 切换
                            if (!flag_needScroll) {
                                flag_needScroll = true;
                                return;
                            }
                            //查询时,删除滚动后重新绑定pageindex
                            if (!flag_InfiniteScroll) {
                                flag_InfiniteScroll = true;
                                return;
                            }
                        });
                    }
                    if (current_tabindex == 0 && $$("#activity_page #sub_stay_tab1 .content-block ul li").length >= data.total) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader1').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }

                }
                else {
                    // 删除加载提示符
                    myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    if (current_tabindex == 0) {
                        $$('.infinite-scroll-preloader1').remove();
                        if (pageindex == 1)
                            $$("#activity_page #sub_stay_tab1 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                }
            },
            error: function (xhr, status) { }
        });
    }
});

var contactsCallback_generalactivityDetail = myApp.onPageInit('activityDetail', function (page) {
    //非微信浏览器下  不显示标题
    if (!ISS.utils.isWeiXin()) {
        mainView.hideNavbar();
        $$('.page-content').attr('style', 'margin-top:-44px')
    }
    //获取页面参数
    var userID = ISS.utils.getParameter("userID");
    if (userID && userID != undefined && userID != null && userID != "") {
        iss_localStorage.set("yuqing_appUserId", userID);
        mainView.hideNavbar();
        $$('.page-content').css('style', 'margin-top:-44px')
    }

    // 过滤
    $$(".navbar-inner .right img").hide();

    //删除滚动后重新添加滚动
    var flag_InfiniteScroll = true;
    //是否需要滚动加载
    var flag_needScroll = true;
    //滚动事件只执行一次
    var flag_execute = false;

    var index_tab1 = 1;
    var current_tabindex = 0;
    var size = 10;

    LoadActivityAll(index_tab1, size);
      //加载数据
    function LoadActivityAll(pageindex, pagesize) {
        var Account = ISS.utils.getParameter("Account");
        var ActivityGuid = ISS.utils.getParameter("ActivityGuid").split('#')[0];
        var _content = JSON.stringify(
                        {
                            AccountGuid: iss_localStorage.get("yuqing_appAccount"),
                            ActivityGuid:ActivityGuid
                            //OpenId: iss_localStorage.get("yuqing_appOpenId")
                        });
        $$.ajax({
            url: ISS_Constants.Url.const_weixin_WechatAcitvity_get,
            data: { conditionParm: _content },
            crossDomain: true,
            dataType: "json",
            success: function (data, status) {
            
                if (data && data.datas && data.datas.length > 0) {
                    for (var i = 0; i < data.datas.length; i++) {
                        data.datas[i].ActivityBeginTime = ISS.utils.dateToStr("yyyy-MM-dd", ISS.utils.strToDate(data.datas[i].ActivityBeginTime));
                        data.datas[i].ActivityEndTime = ISS.utils.dateToStr("yyyy-MM-dd", ISS.utils.strToDate(data.datas[i].ActivityEndTime));

                        data.datas[i].RegistBeginDate = ISS.utils.dateToStr("yyyy-MM-dd", ISS.utils.strToDate(data.datas[i].RegistBeginDate));
                        data.datas[i].RegistEndDate = ISS.utils.dateToStr("yyyy-MM-dd", ISS.utils.strToDate(data.datas[i].RegistEndDate));
                    }
                    iss_localStorage.set("yuqing_headerimg",data.datas[0].ActivityImgUrls.split(';')[0]);
                    if (current_tabindex == 0) {
                        var searchTemplate = $$('#template-generalactivitydetail').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$(".navbar-fixed").html(html);
                            flag_InfiniteScroll = flag_needScroll = true;
                            LoadDT();
                            
                            if (true)//(new Date() >= new Date(data.datas[0].RegistBeginDate) && new Date() <= new Date(data.datas[0].RegistEndDate))
                            {
                                //活动在报名区间
                                 $$('#preOrder').on("click", function (e) {
                                                //取消原有跳转事件
                                                e.stopPropagation();
                                                //保存当前控件
                                                var mythis = $$(this);
                                                //跳转判断
                                                CheckPopLogin(function () {
                                                   
                                                        window.location.href = mythis.attr("data-href");
                                                    
                                                });
                                            });

                            }else if(new Date() >  new Date(data.datas[0].RegistEndDate)){
                                //活动在举行期间
                                $$('#preOrder').attr('data-href', '#')
                                $$('#preOrder').on("click","a", function (e) {
				                $$('#preOrder').attr('data-href','') 	
                                        myApp.alert('活动报名已结束')
                                    });	
                            }else if(new Date() < new Date(data.datas[0].RegistBeginDate)) {
                                //报名未开始
                                $$('#preOrder').attr('data-href', '#')
                                $$('#preOrder').on("click","a", function (e) {
				                $$('#preOrder').attr('data-href','') 	
                                        myApp.alert('活动报名未开始')
                                    });	
                            }else if(new Date() > new Date(data.datas[0].ActivityEndTime)){
                                //活动已经结束
                                $$('#preOrder').attr('data-href', '#')
                                $$('#preOrder').on("click","a", function (e) {
				                $$('#preOrder').attr('data-href','') 	
                                        myApp.alert('活动已结束')
                                });	
                            }
                        }
                    }
                    //tab1综合,无限滚动只加载一次
                    if (!flag_execute) {
                        //只执行一次
                        flag_execute = true;
                        // 加载flag
                        var loading = false;
                        $$('.infinite-scroll').on('infinite', function () {
                            //综合 价格 距离 切换
                            if (!flag_needScroll) {
                                flag_needScroll = true;
                                return;
                            }
                            //查询时,删除滚动后重新绑定pageindex
                            if (!flag_InfiniteScroll) {
                                flag_InfiniteScroll = true;
                                return;
                            }
                        });
                    }
                    if (current_tabindex == 0 && $$("#activity_page #sub_stay_tab1 .content-block ul li").length >= data.total) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader1').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }

                }
                else {
                    // 删除加载提示符
                    myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    if (current_tabindex == 0) {
                        $$('.infinite-scroll-preloader1').remove();
                        if (pageindex == 1)
                            $$("#activity_page #sub_stay_tab1 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                }
            },
            error: function (xhr, status) { }
        });
    }

    
})

var contactsCallback_generalActivityRegister = myApp.onPageInit('generalActivityRegister', function (page) {
    //非微信浏览器下  不显示标题
    if (!ISS.utils.isWeiXin()) {
        mainView.hideNavbar();
        $$('.page-content').attr('style', 'margin-top:-44px')
    }
    
   $$('.page-content-resturant').find('img').attr('src',iss_localStorage.get('headerimg'));
    //隐藏分享按钮
    var gn = ['hideMenuItems'];
    wx_config(gn, function () {
        wx.hideOptionMenu();
    },function(){});

     agent = navigator.userAgent;
    //获取页面参数
    var ActivityGuid = ISS.utils.getParameter("ActivityGuid");

    var userID = ISS.utils.getParameter("userID");
    var OpenId =  iss_localStorage.get("yuqing_appOpenId")
    if (userID && userID != undefined && userID != null && userID != "") {
        iss_localStorage.set("yuqing_appUserId", userID);
        mainView.hideNavbar();
    }

    var gn = ['hideMenuItems'];
    wx_config(gn, function () {
    wx.hideMenuItems({
        menuList: ['menuItem:share:appMessage'] // 要隐藏的菜单项，只能隐藏“传播类”和“保护类”按钮，所有menu项见附录3
    });
    },function(){});


    // 返回
    $$("#generalactivity_register_page .link").on("click", function () {
        var href = $$(this).attr("data-href") + "?scde=" + code;
        $$(this).attr("data-href", href)
        if (href && href != "") {
            window.location.href = href;
        }
    });
     var _content = JSON.stringify(
                        {
                            AccountGuid: iss_localStorage.get("yuqing_appAccount"),
                            ActivityGuid:ActivityGuid
                            //OpenId: iss_localStorage.get("yuqing_appOpenId")
                        });
     var filedtemplate = "<li>"+
                            "<div class='item-content'>"+
                                "<div class='item-inner'>"+
                                    "<div class='item-title label'>"+
                                        "{0}</div>"+
                                    "<div class='item-input'>"+
                                        "<input num='{5}' id='{1}' name='Birthday' guid='{4}' type='{2}' IsRequired={3} placeholder='{0}'/>"+
                                    "</div>"+
                                "</div>"+
                            "</div>"+
                        "</li>";
     $$.ajax({
            url: ISS_Constants.Url.const_weixin_WechatAcitvityFiled_get,
            data: { conditionParm: _content },
            crossDomain: true,
            dataType: "json",
            success: function (data, status) {
                if(data && data.datas && data.datas.length > 0) {
                    filedata = data.datas;
                    var z=0;
                    var html = "";
                    for(var i=0;i<filedata.length;i++){
                        var ftype = filedata[i].FieldType=="1"?"text":"number";
                        html += filedtemplate.format(filedata[i].FieldConent,"txt"+filedata[i].FieldGuid,ftype,filedata[i].IsRequired,filedata[i].FieldGuid,z++);
                    }
                    $$('.list-block ul').html(html);
                   btnbottom = $$('#activityregister-form').offset().top;
                   
          }},
            error: function (xhr, status) { }
        });

    $$('#generalactivity_register_page').on('click','input',function () {  
        if (agent.indexOf('Android') > -1 || agent.indexOf('Adr') > -1) {
            $$('.page-content-resturant').scrollTop(btnbottom+$$(this).height()*$$(this).attr('num'))
        }
    });


    $$('#btnsub').on('click', function () {
        var err = "";
        $$('input[id^=txt]').each(function (i, v) {
            if ($$(v).val() == "" && $$(v).attr('IsRequired')=="1") {
                err = "请填写完整资料"
                return false
            }
            if ($$(v).attr('style') != "" && $$(v).attr('style') != null) {
                err = "请填写正确资料"
                return false
            }
        })
        if (err != "") {
            myApp.alert(err)
            return false;
        }
        var storedData = myApp.formGetData('activityregister-form');
        storedData.AccountGuid = iss_localStorage.get("yuqing_appAccount");
        storedData.OpenId = OpenId;
        storedData.UserID = iss_localStorage.get("yuqing_appUserId");
        storedData.ActivityGuid=ActivityGuid;
        var list = new Array();
        $$('input[id^=txt]').each(function (i, v) {
           var obj = new Object();
           obj.FieldGuid = $$(v).attr('guid');
           obj.FieldValue =  $$(v).val();
           list.push(obj);
        });
        storedData.FPList = list;
        debugger
        var _content = JSON.stringify(storedData);
            $$.ajax({
                url: ISS_Constants.Url.const_weixin_WechatAcitvity_SignUp,
                data: { conditionParm: _content },
                crossDomain: true,
                dataType: "json",
                success: function (data, status) {
                    
                    if (data && data.datas == true) {
                       
                            myApp.alert(data.msg, '', function () {
                                //window.location.href = "generalActivityDetail.html?scde=" + code;
                            });
                        
                    } else {
                        myApp.alert(data.msg);
                    }
                },
                error: function (xhr, status) {
                    console.log(xhr);
                }
            });
    });


});


/* 初始化  */
function initData() {
    var href = window.location.href;
    if (href.indexOf(activity) > -1) {
        contactsCallback_generalactivity.trigger();
    }
    else if (href.indexOf(activityDetail) > -1) {
        contactsCallback_generalactivityDetail.trigger();
    } else if (href.indexOf(generalActivityRegister) > -1) {
        contactsCallback_generalActivityRegister.trigger();
    }
}
function LoadDT(){
    InitControl()
    var mySequence1 = [
       { elements: $('#activetitle'), properties: { opacity: [0.7, 0], translateY: [0, -80] }, options: { duration: 900 },begin: function (elements) { console.log($('#activetitle')); $('#div1').attr('style', 'display:none') }, },
       { elements: $('#activetime'), properties: { opacity: [0.7, 0], translateY: [0, -80] }, options: { duration: 900} },
       { elements: $('#name_img'), properties: { opacity: [0.8, 0], translateX: [0, -90] }, options: { duration: 300} },
       { elements: $('#div1'), properties: { opacity: 1 }, options: { duration: 900} },
       { elements: $('#zbdw'), properties: { opacity: [0.8, 0], translateX: [0, -90] }, options: { duration: 900} },
       { elements: $('#cbdw'), properties: { opacity: [0.8, 0], translateX: [0, 90] }, options: { duration: 900} },
       { elements: $('#arrow'), properties: {  opacity: [0, 0.8],translateY: [-30, 0] }, options: { duration: 1000, loop: true} }
    ];
    var mySequence2 = [
        { elements: $('#intromain-tit'), properties: { opacity: [0.7, 0], translateY: [0, -80] }, options: { duration: 500} },
        { elements: $('#intro-img-02'), properties: { opacity: [0.8, 0.1],rotateZ:360, translateX: [0, 180] }, options: { duration: 500} },
        { elements: $('#intro-img-03'), properties: { opacity: [0.8, 0.1], translateY: 20 }, options: { duration: 600} },
      
        { elements: $('#intro-img-04'), properties: { opacity: [0.8, 0.1], translateY: [0, 40] }, options: { duration: 600} },
        { elements: $('#intro-img-01'), properties: { opacity: [0.8, 0.1], translateX: [0, 20] }, options: { duration: 700} },
        { elements: $('#content-block'),properties: { opacity: [0.8, 0.1]}, options: { duration: 800} },
        { elements: $('#arrow1'), properties: {opacity: [0, 0.8], translateY: [-30, 0] }, options: { duration: 1000, loop: true} }
    ];
    var mySequence3 = [ 
        {elements: $('#nodemain-tit'), properties: { opacity: [0.7, 0], translateY: [0, -80] }, options: { duration: 500} },
        {elements: $('#circle-s'), properties: { opacity: [0.8, 0] }, options: { duration: 300,delay:1}},
        {elements: $('#circle-m'), properties: { opacity: [0.8, 0] }, options: { duration: 500,delay:3}},
        {elements: $('#circle-l'), properties: { opacity: [0.8, 0] }, options: { duration: 700,delay:5}},
        {elements: $('.activityNote #content-block'),properties: { opacity: [0.8, 0.1]}, options: { duration: 800} },
        {elements: $('#arrow1'), properties: {opacity: [0, 0.8], translateY: [-30, 0] }, options: { duration: 1000, loop: true} }
    ];
    var mySequence4 = [
        {elements: $('#timemain-tit'), properties: { opacity: [0.7, 0], translateY: [0, -80] }, options: { duration: 500} },
        {elements: $('.activityTime #content-block'),properties: { opacity: [0.8, 0]}, options: { duration: 800} },
        {elements: $('#time-img'),properties: { opacity: [0.8, 0]}, options: { duration: 800} },
        
//        {elements: $('#circle-s'), properties: { opacity: [0.8, 0] }, options: { duration: 300,delay:1}},
//        {elements: $('#circle-m'), properties: { opacity: [0.8, 0] }, options: { duration: 500,delay:3}},
//        {elements: $('#circle-l'), properties: { opacity: [0.8, 0] }, options: { duration: 700,delay:5}}
    ]

    $.Velocity.RunSequence(mySequence1);
    var slider = $.fn.fsvs({
        autoPlay: false,
        speed: 1000,
        bodyID: 'fsvs-body',
        mouseSwipeDisance: 80,

        beforeSlide: function (index) {// 滚动开始
            InitControl()
            if (index == 0) {
                $.Velocity.RunSequence(mySequence1);
            } else if (index == 1) {
                $.Velocity.RunSequence(mySequence2);
            } else if (index == 2) {
                $.Velocity.RunSequence(mySequence3);
              //circle()
            } else if (index == 3) {
                 $.Velocity.RunSequence(mySequence4);
            }
        },
        afterSlide: function (index) {// 滚动结束

        },
        endSlide: function (index) {// 滚动到底

        },
        mouseWheelEvents: true,
        mouseWheelDelay: false,
        mouseDragEvents: true,
        touchEvents: true,
        arrowKeyEvents: true,
        pagination: true,
        nthClasses: 2,
        detectHash: true
    });
}

function InitControl()
{
     $('#activetitle').velocity({
        begin: function (elements) { 
        $('#activetitle').attr('style', 'opacity:0') 
        $('#activetime').attr('style', 'opacity:0') 
        $('#name_img').attr('style', 'opacity:0') 
        $('#div1').attr('style', 'opacity:0') 
        $('#zbdw').attr('style', 'opacity:0') 
        $('#cbdw').attr('style', 'opacity:0') 
        $('#arrow').attr('style', 'display: block; width: 7%; height: auto; bottom: 18px; position: absolute; left: 50%;opacity:0') 
        }
    });
     $('#intromain-tit').velocity({
        begin: function (elements) { 
        $('#main-tit').attr('style', 'opacity:0') 
        $('#intro-img-02').attr('style', 'opacity:0') 
        $('#intro-img-03').attr('style', 'opacity:0') 
        $('#intro-img-04').attr('style', 'opacity:0') 
        $('#intro-img-01').attr('style', 'opacity:0') 
        $('.activityNote #content-block').attr('style', 'opacity:0') 
        $('#arrow1').attr('style', 'display: block; width: 7%; height: auto; bottom: 18px; position: absolute; left: 50%;opacity:0') 
        }
    });
     $('#nodemain-tit').velocity({
        begin: function (elements) { 
        $('#nodemain-tit').attr('style', 'opacity:0') 
        $('#circle-s').attr('style', 'opacity:0') 
        $('#circle-m').attr('style', 'opacity:0') 
        $('#circle-l').attr('style', 'opacity:0') 
        $('#content-block').attr('style', 'opacity:0') 
        $('#arrow2').attr('style', 'display: block; width: 7%; height: auto; bottom: 18px; position: absolute; left: 50%;opacity:0') 
        }
     });
     $('#timemain-tit').velocity({
            begin: function (elements) {
                $('#timemain-tit').attr('style', 'opacity:0') 
                $('#time-img').attr('style', 'opacity:0')
                $('.activityTime #content-block').attr('style', 'opacity:0') 
            }
     })
}
initData();
